package com.chartHandle.excelService.handle;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.chartHandle.bean.CellStyleWY;
import com.chartHandle.bean.MergeBean;
import com.chartHandle.bean.PointXY;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.util.Map;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/11/10/15:23
 * @target 心若有所向往，何惧道阻且长
 */
// 合并的是 档位扩展投放中的字段 价格段自选投放的价格段投放上限
public class WeeklySupplyStrategyDataMerge extends AbstractMergeStrategy {
    private Map<PointXY, MergeBean> mergeBeans;

    public WeeklySupplyStrategyDataMerge(Map<PointXY, MergeBean> mergeBeans) {
        this.mergeBeans = mergeBeans;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        if (integer == null || integer == 0) {
            return;
        }
        // 列的索引
        int rowIndex = cell.getRowIndex();
        // 行的索引
        int colIndex = cell.getColumnIndex();
        sheet = cell.getSheet();
        MergeBean mergeBean = mergeBeans.get(new PointXY(colIndex, rowIndex));
        if (mergeBean != null) {
            CellRangeAddress cra = new CellRangeAddress(mergeBean.getStart().getY(), mergeBean.getEnd().getY(), mergeBean.getStart().getX(), mergeBean.getEnd().getX());
            sheet.addMergedRegion(cra);
            CellStyleWY styleWY = mergeBean.getStyleWY();
            // 获取合并单元格的样式
            CellStyle cellStyle = cell.getCellStyle();
            //设置合并区域cra的边框
            if (styleWY != null) {
                if (styleWY.getUpBorderStyle() != null) {
                    RegionUtil.setBorderTop(styleWY.getUpBorderStyle(), cra, sheet);
                }
                if (styleWY.getDownBorderStyle() != null) {
                    RegionUtil.setBorderBottom(styleWY.getDownBorderStyle(), cra, sheet);
                }
                if (styleWY.getLeftBorderStyle() != null) {
                    RegionUtil.setBorderLeft(styleWY.getLeftBorderStyle(), cra, sheet);
                }
                if (styleWY.getRightBorderStyle() != null) {
                    RegionUtil.setBorderRight(styleWY.getRightBorderStyle(), cra, sheet);
                }
                if (styleWY.getHorizontalAlignment() != null) {
                    cellStyle.setAlignment(styleWY.getHorizontalAlignment());
                }
                if (styleWY.getVerticalAlignment() != null) {
                    cellStyle.setVerticalAlignment(styleWY.getVerticalAlignment());
                }
                if (styleWY.getHorizontalAlignment() != null || styleWY.getVerticalAlignment() != null) {
                    // 只更新第一个单元格的样式
                    Row firstRow = sheet.getRow(cra.getFirstRow());
                    if (firstRow != null) {
                        Cell firstCell = firstRow.getCell(cra.getFirstColumn());
                        if (firstCell != null) {
                            firstCell.setCellStyle(cellStyle);
                        }
                    }
                }
            }
        }
    }
}
